package com.cy.ppj.service.order.impl;

import com.cy.ppj.comm.constant.CacheConstants;
import com.cy.ppj.dao.mapper.order.OrderMapper;
import com.cy.ppj.dao.mapper.order.OrderWithholdMapper;
import com.cy.ppj.dao.mapper.order.WithholdRecordMapper;
import com.cy.ppj.model.Result;
import com.cy.ppj.model.domian.order.WithholdRecord;
import com.cy.ppj.model.enums.OrderWithholdStatus;
import com.cy.ppj.model.enums.UniqueKeyType;
import com.cy.ppj.model.enums.WithholdStatus;
import com.cy.ppj.model.vo.admin.order.OrderWithholdAdminStatisticsVO;
import com.cy.ppj.service.BaseService;
import com.cy.ppj.service.comm.UniqueKeyService;
import com.cy.ppj.service.order.WithholdRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @description: 代扣记录
 * @author: Ocean
 * @create: 2018-08-03 21:59
 **/
@Slf4j
@Service
public class WithholdRecordServiceImpl extends BaseService implements WithholdRecordService {

    @Autowired
    private WithholdRecordMapper withholdRecordMapper;
    @Autowired
    private UniqueKeyService uniqueKeyService;
    @Autowired
    private OrderWithholdMapper orderWithholdMapper;
    @Autowired
    private OrderMapper orderMapper;

    @Override
    @Transactional
    public void insert(WithholdRecord withholdRecord) {
        withholdRecord.setStatus(WithholdStatus.PROCESSING);
        withholdRecordMapper.insertSelective(withholdRecord);

        //订单更改为代扣中
        orderMapper.updateWithholdStatus(withholdRecord.getOrderId(), OrderWithholdStatus.WITHHOLD_ING);

        //创建唯一约束
        Result r = uniqueKeyService.create(UniqueKeyType.WITHHOLD, String.valueOf(withholdRecord.getOrderId()));
        state(r.isOk(), "该订单已提交代扣，正在处理中");
    }

    @Override
    @Transactional
    public void updateStatus(Long id, WithholdStatus status) {

        WithholdRecord withholdRecord = withholdRecordMapper.selectByPrimaryKey(id);
        state(withholdRecord != null, "数据异常");
        withholdRecord.setStatus(status);

        int result = withholdRecordMapper.updateStatus(withholdRecord.getId(), withholdRecord.getStatus());
        state(result == 1, "更新代扣记录状态失败");

        OrderWithholdStatus orderWithholdStatus = OrderWithholdStatus.WITHHOLD_SUCCESS;

        if (WithholdStatus.FAIL.equals(withholdRecord.getStatus())) {
            orderWithholdStatus = OrderWithholdStatus.WITHHOLD_FAIL;
            Result r = uniqueKeyService.discard(UniqueKeyType.WITHHOLD, String.valueOf(withholdRecord.getOrderId()));
            state(r.isOk(), "系统异常");
        }

        //订单更改为代扣中
        orderMapper.updateWithholdStatus(withholdRecord.getOrderId(), orderWithholdStatus);
    }

    @Override
    @Cacheable(value = CacheConstants.CACHE_VALUE_15S)
    public List<OrderWithholdAdminStatisticsVO> orderWithholdAdminStatisticsInCache() {
        return orderWithholdMapper.selectOrderWithholdAdminStatistics();
    }
}
